// axios的封装 为了可以方便的统一的使用axios做数据请求
import axios from "axios";
import qs from "qs";
// import store from '../store/index'
import router from "@/router/index.js";
import { Message } from "element-ui";
import store from "@/store";
import { getToken } from "@/utils/auth";
import { baseUrl } from "@/utils/myUtils";
const service = axios.create({
  // withCredentials: true,
  baseURL: baseUrl,
  // baseURL: "http://172.16.22.171:8901",
  timeout: 80000
});
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
service.interceptors.request.use(
  config => {
    // let token = getToken() || "";
    config.headers = {
      // "Content-Type": "application/json;charset=UTF-8",
      token: localStorage.getItem("mytoken") || ""
    };
    return config;
  },
  error => {
    return Promise.reject(err);
  }
);
service.interceptors.response.use(
  response => {
    let code = response.data.code;
    let data = response.data.data;
    let message = response.data.message;
    console.log(response, "拦截", code, message);
    switch (code) {
      case 200:
        if (data instanceof Array) {
          data.forEach(item => {
            item.createTime = timestampToTime(item.createTime);
          });
        }

        return Promise.resolve(response);
        break;
      case 500:
        Message({
          message: message || "请刷新页面",
          type: "warning",
          duration: 5 * 1000
        });
        return Promise.reject(message);
        break;
      case 401:
        Message({
          message: message || "请刷新页面",
          type: "warning",
          duration: 5 * 1000
        });
        return Promise.reject(message);
        break;
      case 403:
        Message({
          message: "登录已过期,请从新登录",
          type: "error",
          duration: 5 * 1000
        });
        localStorage.removeItem("mytoken");

        router.replace({
          path: "/login"
        });
        return Promise.reject(message);
        break;
      default:
        break;
    }
    if (response.data == "") {
      Message({
        message: "错误,请联系管理员",
        type: "warning",
        duration: 5 * 1000
      });
      return;
    }
  },
  error => {
    console.log(error, "zheli zhixi");
    Message({
      message: "发生错误,请联系管理员",
      type: "warning",
      duration: 5 * 1000
    });
    // localStorage.removeItem("mytoken");
    // if (error) {
    //   router.replace({
    //     path: "/login"
    //   });
    // }
    return Promise.reject(error);
  }
);
function http(config) {
  if (config.method.toLowerCase() === "post") {
    // config.data = qs.stringify(config.data, {
    //   arrayFormat: "repeat",
    //   allowDots: true,
    //   allowDots: true
    // });
  } else {
    config.params = config.data;
  }
  return service(config);
}
export default http;
function timestampToTime(timestamp) {
  var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + "-";
  var M =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1) + "-"
      : date.getMonth() + 1 + "-";
  var D =
    date.getDate() < 10 ? "0" + date.getDate() + " " : date.getDate() + " ";
  var h =
    date.getHours() < 10 ? "0" + date.getHours() + ":" : date.getHours() + ":";
  var m =
    date.getMinutes() < 10
      ? "0" + date.getMinutes() + ":"
      : date.getMinutes() + ":";
  var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}
